import 'package:flutter_learn_vi/src/net/dio_utils.dart';
import 'package:flutter_learn_vi/src/utils/toast_utils.dart';
import 'package:get/get.dart';

import '../../../bean/bean_order.dart';
import '../../../bean/bean_study_data.dart';

class MyStudyDataController extends GetxController {
  int totalCount = 0;
  int totalPage = 0;
  int currPage = 0;
  int pageSize = 30;
  List<MaterialCategoryBean> materialCategoryList = [];
  List<StudyDataBean> studyDataBeanList = [];

  bool _isLoading = true;
  bool _isListLoading = true;

  Future<bool> requestCategoryList() async {
    Map<String, dynamic> map = Map();
    ResponseInfo responseInfo = await DioUtils.instance.getRequest(
        url: HttpHelper.materialCategoryListUrl, queryParameters: map);
    _isLoading = false;
    materialCategoryList = [];
    if (responseInfo.success) {
      List list = responseInfo.data;
      list.forEach((element) {
        MaterialCategoryBean newsBean = MaterialCategoryBean.fromMap(element);
        materialCategoryList.add(newsBean);
      });
      requestMaterialList(0);
    }
    update(["Category"]);
    return true;
  }

  Future<bool> requestMaterialList(int currentIndex) async {
    Map<String, dynamic> map = Map();
    map["videoCourseId"] = materialCategoryList[currentIndex].id;
    map["page"] = 1;
    map["limit"] = 100;

    ResponseInfo responseInfo = await DioUtils.instance
        .getRequest(url: HttpHelper.materialListUrl, queryParameters: map);
    _isListLoading = false;
    studyDataBeanList = [];
    if (responseInfo.success) {
      dynamic list = responseInfo.data["list"];
      if (list != null && list is List) {
        list.forEach((element) {
          StudyDataBean newsBean = StudyDataBean.fromMap(element);
          studyDataBeanList.add(newsBean);
        });
      }
    }
    update(["MaterialList"]);
    return true;
  }

  Future<bool> onClickRefresh() async {
    _isLoading = true;
    update(["Category"]);
    await requestCategoryList();
    return true;
  }

  Future<bool> onClickItemRefresh(int currentIndex) async {
    _isListLoading = true;
    update(["MaterialList"]);
    await requestMaterialList(currentIndex);
    return true;
  }

  Future<bool> onPullRefresh({int index = 0}) async {
    await requestCategoryList();
    return true;
  }

  Future<bool> onRefresh({int index = 0}) async {
    await requestCategoryList();
    return true;
  }

  bool get isLoading => _isLoading;

  bool get isListLoading => _isListLoading;

  void updateIndex(int currentIndex) {
    update(["Category"]);
    requestMaterialList(currentIndex);
  }
}
